package com.wontube.smr.web.controller.mg;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.vo.AudioFiles;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.web.client.IMVWebHandler;
import com.wontube.smr.web.client.IMp3WebHandler;

/**
 * mobile go music(mp3)
 * 
 * @author hewc@wondershare.cn
 * 
 */
@Controller
@RequestMapping("/mg/mp3")
public class MgMusicController {

	private final static int MP3_PAGE_SIZE = 50;

	@RequestMapping("")
	public void v4(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/mg/mp3/");
	}

	@RequestMapping("/")
	public ModelAndView music_mp3() {
		ModelAndView mv = new ModelAndView("/mg/mp3/mp3_index");
		IMVWebHandler handler = getMvHandler();
		// vevoMusics && vevoArtists
		Map<String, Object> maps = handler.getMp3Home();
		mv.addAllObjects(maps);
		return mv;
	}

	@RequestMapping("/list/{page}")
	public ModelAndView listMusics(@RequestParam(value = "ajax", required = false) String ajax,
			@PathVariable("page") Integer page) {
		if (page.intValue() < 1) {
			page = 1;
		}

		ModelAndView mv = new ModelAndView();
		IMVWebHandler handler = getMvHandler();
		MovieItems musicItems = handler.listVevoMusics(new PagenateInfo(page, MP3_PAGE_SIZE));
		mv.addObject("vevoMusics", musicItems);

		if (ajax != null) {
			mv.setViewName("/mg/mp3/mp3_list");
		} else {
			mv.setViewName("/mg/mp3/mp3_index");
			mv.addObject("page", page);
			mv.addObject("vevoArtists", handler.listVevoTopArtists(new PagenateInfo(1, 25)));
		}

		return mv;
	}

	@RequestMapping("/search")
	public ModelAndView searchMp3(@RequestParam("query") String query,
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
		ModelAndView mv = new ModelAndView("/mg/mp3/mp3_search_result");
		Remoting<IMp3WebHandler> remoting = new Remoting<IMp3WebHandler>();
		remoting.setReadTimeout(60000); // 查询较慢
		IMp3WebHandler handler = remoting.createService(IMp3WebHandler.class);
		AudioFiles files = handler.search(query, page);
		mv.addObject("audioFiles", files);
		mv.addObject("query", query);
		mv.addObject("page", page);
		return mv;
	}

	@RequestMapping("/{query}")
	public ModelAndView searchMusic(@PathVariable("query") String query) {
		return musicMp3Query(query);
	}

	@RequestMapping("/{query}/{page}")
	public ModelAndView searchMusic2(@PathVariable("query") String query, @PathVariable("page") Integer page) {
		return mp3Search(query, page);
	}

	@RequestMapping("/mp3/{query}")
	public ModelAndView musicMp3Query(@PathVariable("query") String query) {
		ModelAndView mv = mp3Search(query, 1);
		return mv;
	}

	@RequestMapping(value = "/mp3/{query}/{page}", method = RequestMethod.GET)
	public ModelAndView mp3Search(@PathVariable("query") String query, @PathVariable("page") Integer page) {
		Remoting<IMp3WebHandler> remoting = new Remoting<IMp3WebHandler>();
		remoting.setReadTimeout(60000); // 查询较慢
		IMp3WebHandler handler = remoting.createService(IMp3WebHandler.class);
		ModelAndView mv = new ModelAndView();
		mv.addObject("searchMusic", true);
		if (!"".equals(query)) {
			AudioFiles files = handler.search(query, page);
			mv.addObject("audioFiles", files);
			mv.setViewName("/mg/mp3/mp3_search_index");
		}
		mv.addObject("query", query);
		mv.addObject("page", page);
		// String upstreamUrl = remoting.getHeaders().get("who");
		// mv.addObject("serverUrl", upstreamUrl);

		return mv;
	}

	private IMVWebHandler getMvHandler() {
		Remoting<IMVWebHandler> remoting = new Remoting<IMVWebHandler>();
		return remoting.createService(IMVWebHandler.class);
	}


}
